import Vue from 'vue'
import Router from 'vue-router'
import store from "../store/store.js"
// import index from '@/src/components/children/yusuan_GL/yusuan_GL.vue'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

//首页
const index=()=>import("@/components/index/index.vue")
// 登录页
const login=()=>import("@/components/login/login.vue")
// 404页面
const error=()=>import("@/components/error/error.vue")
// 代办中心
const daibanZX=()=>import("@/components/children/daibanZX/daibanZX.vue")
// 红冲核销管理
const hongchongHX=()=>import("@/components/children/hongchongHX/hongchongHX.vue")
// 创建PO核销
const add_HX=()=>import("@/components/children/add_HX/add_HX.vue")
// po核销列表
const po_hexiaoLB=()=>import("@/components/children/po_hexiaoLB/po_hexiaoLB.vue")
// 合同核销管理
const hetong_HX=()=>import("@/components/children/hetong_HX/hetong_HX.vue")
// 核销管理
const HX_guanli=()=>import("@/components/children/HX_guanli/HX_guanli.vue")
// 预算管理
const yusuan_GL=()=>import("@/components/children/yusuan_GL/yusuan_GL.vue")
// POPATO个人设置中心
const user=()=>import("@/components/children/user/user.vue")

Vue.use(Router)

const routes=[
  {
    path: '/',
    component: index,
    meta:{
      title:"POPATO管理系统"
    },
    children:[
      {
      	path:'',
      	redirect:'daibanZX',
      	component:daibanZX,
      },
      {
        path:'daibanZX',
        name:"daibanZX",
        component:daibanZX,
        meta:{
          title:"代办中心",
          isAuth:true
        }
      },
      {
        path:'add_HX',
        name:"add_HX",
        component:add_HX,
        meta:{
          title:"创建PO核销",
          isAuth:true
        }
      },
      {
        path:'po_hexiaoLB',
        name:"po_hexiaoLB",
        component:po_hexiaoLB,
        meta:{
          title:"PO核销列表",
          isAuth:true
        }
      },
      {
        path:'hongchongHX',
        name:"hongchongHX",
        component:hongchongHX,
        meta:{
          title:"红冲核销管理",
          isAuth:true
        }
      },
      {
        path:'hetong_HX',
        name:'hetong_HX',
        component:hetong_HX,
        meta:{
          title:"合同核销管理",
          isAuth:true
        }
      },
      {
        path:'HX_guanli',
        name:'HX_guanli',
        component:HX_guanli,
        meta:{
          title:"核销管理",
          isAuth:true
        }
      },
      {
        path:'yusuan_GL',
        name:"yusuan_GL",
        component:yusuan_GL,
        meta:{
          title:"预算管理",
          isAuth:true
        }
      },
      {
        path:"user",
        name:'user',
        component:user,
        meta:{
          title:"个人中心",
          isAuth:true
        }
      }
    ]
  },
  {
    path:'/login',
    name:login,
    component:login,
    meta:{
      title:"菲仕兰活动核销系统—登录注册"
    }
  },
  {
    path:"*",
    name:error,
    component:error,
    meta:{
      title:'404'
    }
  }
]
const route=new Router({
  routes,
  mode:'history'//history  hash
})

// 路由守卫设置title
route.beforeEach((to,from,next)=>{
  // 跳转路由展示对应的title
  window.document.title = to.meta.title;
  //获取vuex中的token,是否处于登录的状态
  const token=store.state.token ? store.state.token:window.localStorage.getItem("token");
  // console.log(token,"token")
  //判断该路由是否需要登录权限
  if(to.meta.isAuth){
    //通过vuex中的state获取当前的token是否存在
    if(token=="" || token==null){
      next({
        path:'/login',
      })
    }else{
      next();
    }
  }else{
    console.log("Paolo")
    next();
  }


})
export default route
